/*
 * @Description:
 * @Author: mmzhang
 * @Date: 2022-01-18 10:19:15
 * @LastEditTime: 2022-08-11 15:44:50
 */
import toolMgr from '@/biz/base/tool.js'
import dt from '@dt/dt'
import ruleOpt from '@/biz/option/rule.js'

let _this = null
// 渠道名称
function part1() {
  return [
    {
      title: '渠道名称',
      placeholder: '请输入渠道名称',
      tips:
        _this.operateType == '0'
          ? `渠道创建后<span>名称不可修改</span>，若名称过长，请使用可辨识的简称`
          : null,
      tipsIsBlock: 'Dinline',
      key: 'channelName',
      disabled: _this.operateType == 0 ? false : true,
      input: {},
      rules: [ruleOpt.required('渠道名称不能为空'), ruleOpt.max(20)]
    }
    // {
    //   title: '',
    //   placeholder: '',
    //   key: 'nameTips',
    //   text: {
    //     color: '#999',
    //     textAlign: 'right'
    //   },
    // }
  ]
}
// 合作日期
function part3() {
  return [
    {
      title: '合作开始日期',
      placeholder: '请选择日期',
      key: 'cooperationStartDate',
      disabled: _this.operateType == 2 ? true : false,
      date: {}
    },
    {
      title: '合作结束日期',
      placeholder: '请选择日期',
      tips: `如选择合作日期，则表示该渠道仅在对应日期内有效，过期后顾问将不可选择。如不填写，则表示永久有效`,
      tipsIsBlock: 'Dinline',
      key: 'cooperationEndDate',
      disabled: _this.operateType == 2 ? true : false,
      date: {}
    }
    // {
    //   title: '',
    //   placeholder: '',
    //   key: 'tips',
    //   text: {
    //     color: '#999',
    //     textAlign: 'right'
    //   },
    // }
  ]
}
// 渠道费用
function channelCost() {
  function rules() {
    let arr = [ruleOpt.required(text(), 'number'), ruleOpt.twoDecimal(), ruleOpt.positiveNumber()]
    if (_this.channelRelated.channelCostType == '2') {
      arr.push(ruleOpt.percent())
    }
    if (_this.channelRelated.channelCostType != '3') {
      return arr
    } else {
      // return [
      //   ruleOpt.required('请输入费用类型'),
      // ]
    }
  }

  function text() {
    return _this.channelRelated.channelCostType == '3'
      ? ' '
      : _this.channelRelated.channelCostType == '1'
      ? '请输入固定金额'
      : '请输入比例'
  }

  function unit() {
    return _this.channelRelated.channelCostType == '3'
      ? ' '
      : _this.channelRelated.channelCostType == '1'
      ? '元'
      : '%'
  }

  function group() {
    return [
      {
        key: 'channelCostType',
        tips:
          _this.operateType == '0' ? `如果渠道费用不固定，请选择“浮动”，可在审核认购单时填写具体金额` : null,
        tipsIsBlock: 'Dinline',
        col: 8,
        select: {
          options: dt.dictInfo().channelCostType
        },
        disabled: _this.operateType == 2 ? true : false,
        change: () => {
          _this.channelRelated.channelCostValue = null
          opt.group = group()
          opt.rules = rules()
        }
      },
      {
        key: 'channelCostValue',
        col: 8,
        placeholder: text(),
        visible: _this.channelRelated.channelCostType != '3',
        disabled: _this.operateType == 2 ? true : false,
        input: {
          type: _this.channelRelated.channelCostType == '3' && _this.operateType == 2 ? 'text' : 'number'
        }
      },
      {
        placeholder: unit(),
        col: 8,
        text: {}
      }
    ]
  }

  let opt = {}
  if (_this.operateType != 2) {
    opt = {
      title: '渠道费用',
      key: 'channelCostValue',

      group: group(),
      rules: rules()
    }
  } else {
    opt = {
      title: '渠道费用',
      key: 'channelCostValue',
      group: [
        {
          key: 'channelCostValue',
          col: 8,
          placeholder: text(),

          disabled: _this.operateType == 2 ? true : false,
          input: {
            type: _this.channelRelated.channelCostType == '3' && _this.operateType == 2 ? 'text' : 'number'
            // type: _this.channelRelated.channelCostType == '3' ? '' : 'number'
          }
        },
        {
          placeholder: unit(),
          col: 8,
          text: {}
        }
      ]
    }
  }

  return opt
}

// 合同资料
function channelAnnexEdit() {
  let api = toolMgr.unloadFile()
  let files = []
  let channelAnnex = JSON.parse(_this.channelRelated.channelAnnex)
  if (channelAnnex && channelAnnex.length > 0) {
    files = channelAnnex
  }
  return {
    title: '合同资料',
    key: 'channelAnnex',
    disabled: _this.operateType == 2 ? true : false,
    upload: {
      title: '附件上传',
      api,
      files,
      multiple: true,
      preview: (file) => {
        toolMgr.downloadFile(file.url, file.name)
      }
    },
    data: {
      to: (v) => {
        let channelAnnexList = v.map((item) => {
          return {
            name: item.name,
            url: item.response ? item.response.data.imageURL : item.url
          }
        })
        let channelAnnex = JSON.stringify(channelAnnexList)
        return {
          channelAnnex
        }
      }
    }
  }
}

function channelAnnexView() {
  let files = []
  let channelAnnex = JSON.parse(_this.channelRelated.channelAnnex)
  if (channelAnnex && channelAnnex.length > 0) {
    files = channelAnnex.map((item) => {
      return {
        value: item.name,
        key: item.url
      }
    })
  }
  _this.channelRelated.channelAnnexTip = '暂无'
  let arr = []
  if (files.length) {
    arr = [
      {
        title: '合同资料',
        tag: {
          closable: false,
          options: files,
          color: 'blue',
          click: (tag) => {
            toolMgr.downloadFile(tag.key, tag.value)
          }
        }
      }
    ]
  } else {
    arr = [
      {
        title: '合同资料',
        placeholder: '',
        key: 'channelAnnexTip',
        text: {}
      }
    ]
  }
  return arr
}
// oa
function part4() {
  let arr = []
  if (_this.operateType == 2) {
    arr = [
      channelCost(),
      channelAnnexView(),
      {
        title: 'OA营销采购申请单',
        placeholder: '请输入OA营销采购申请单',
        key: 'oaProcurement',
        input: {},
        disabled: _this.operateType == 2 ? true : false
      }
    ]
  } else {
    arr = [
      channelCost(),
      channelAnnexEdit(),
      {
        title: 'OA营销采购申请单',
        placeholder: '请输入OA营销采购申请单',
        key: 'oaProcurement',
        input: {},
        disabled: _this.operateType == 2 ? true : false
      }
    ]
  }
  return arr
}
// 渠道状态
function part5() {
  return [
    {
      title: '渠道状态',
      key: 'validate',
      radio: {
        options: [
          {
            key: 1,
            value: '启用',
            disabled: _this.operateType == 2 ? true : false
          },
          {
            key: 2,
            value: '禁用',
            disabled: _this.operateType == 2 ? true : false
          }
        ]
      },
      rules: [ruleOpt.required('请选择渠道状态', 'number')]
    },
    {
      title: '备注',
      key: 'remarks',
      placeholder: '请输入备注',
      input: {
        type: 'textarea',
        autosize: {
          minRows: 2
        }
      },
      disabled: _this.operateType == 2 ? true : false,
      rules: [ruleOpt.max(300)]
    }
  ]
}

// 自获客打法
function part6() {
  return [
    {
      title: '自获客打法',
      key: 'planIdArr',
      disabled: _this.operateType == 2 ? true : false,
      cascader: {
        options: _this.planChannelChannelDataArr
      },
      change: (val) => {
        console.log(val, _this.channelRelated, 'vvvvvvvvvvvv')
        _this.channelRelated.planIdArr = val.value
        _this.channelRelated.planId = val.value[1]
      },

      rules: [
        {
          type: 'array',
          required: true,
          message: '请选择自获客打法',
          trigger: 'change'
        }
      ]
    }
  ]
}

function options(vm) {
  _this = vm
  let cooperationTypeFinal = JSON.parse(JSON.stringify(dt.dictInfo().cooperationTypeFinal))
  let _idx = cooperationTypeFinal.findIndex((item) => {
    return item.key == '-1'
  })

  if (_idx > 0) {
    cooperationTypeFinal.splice(_idx, 1)
  }

  let _idx1 = cooperationTypeFinal.findIndex((item) => {
    return item.key == '6'
  })

  if (_idx1 > 0) {
    cooperationTypeFinal.splice(_idx1, 1)
  }

  // let channelCostType = JSON.parse(JSON.stringify(dt.dictInfo().channelCostType))
  // if (_this.operateType == 2) {
  //   // cooperationTypeFinal.push({
  //   //   value: "待渠道确认",
  //   //   // dictLabelCn: "待渠道确认",
  //   //   // dictValue: "6",
  //   //   key: "6"
  //   // })
  //   channelCostType.push({
  //     value: "浮动",
  //     // dictLabelCn: "浮动",
  //     // dictValue: "3",
  //     key: "3"
  //   })
  // } else {
  //   channelCostType = dt.dictInfo().channelCostType
  //   let _idx = cooperationTypeFinal.findIndex(item => {
  //     return item.key == '6'

  //   })
  //   if (_idx > 0) {
  //     cooperationTypeFinal.splice(_idx, 1)
  //   }
  // }

  function rules() {
    let arr = [ruleOpt.required('合作开始日期不能为空')]
    return arr
  }

  let arr = []

  let p1 = part1()
  let p3 = part3()
  let p4 = part4()
  let p5 = part5()
  let p6 = part6()

  // let channelTypeObj = {
  //   title: '渠道分类',
  //   placeholder: '请选择',
  //   key: 'channelType',
  //   select: {
  //     options: dt.dictInfo().channelType
  //   },
  //   disabled: _this.operateType == 0 ? false : true,
  //   change: (val) => {
  //     // _this.clearData(val)
  //     val.info.cooperationEndDate = ''
  //     val.info.cooperationStartDate = ''
  //     val.info.channelName = ''
  //     // val.info.cooperationStartDate = ''
  //     // val.info.cooperationStartDate = ''

  //     if (val.value == 2 || val.value == 3) {
  //       arr.splice(3, arr.length)
  //       arr.push(cooperationTypeObj)
  //       arr.push(...p3)
  //       arr.push(...p4)
  //       arr.push(...p5)
  //     } else {
  //       arr.splice(3, arr.length)
  //       arr.push(...p5)
  //     }
  //   },
  //   rules: [
  //     ruleOpt.required('渠道分类不能为空')
  //   ]
  // }

  let isTakeLookObj = {
    title: '是否渠道带看',
    key: 'isTakeLook',
    disabled: _this.operateType == 0 ? false : true,
    radio: {
      options: [
        { key: '0', value: '否' },
        { key: '1', value: '是' }
      ]
    },
    change: (val) => {
      console.log(val, '渠道带看')
      if (val.value == '1') {
        _this.channelRelated.isNetAcquire = '0'
      }
      if (_this.channelRelated.isTakeLook == '1' || _this.channelRelated.isNetAcquire == '1') {
        let _index = arr.findIndex((item) => {
          return item.key == 'cooperationType'
        })
        if (_index == '-1') {
          arr.splice(4, 0, cooperationTypeObj, ...p3)
        }
        if (_this.channelRelated.cooperationType == 1) {
          // arr.splice(4, arr.length)
          // p3[0].rules = rules()
          // p3[1].rules = rules()
          arr.splice(7, arr.length, ...p4, ...p5)
        } else {
          arr.splice(7, arr.length, ...p5)
        }
      } else {
        arr.splice(4, arr.length, ...p5)
      }
    },
    rules: [
      {
        required: true,
        message: '请选择是否渠道带看',
        trigger: 'blur'
      }
    ]
  }

  let isNetAcquireObj = {
    title: '是否网络获客',
    key: 'isNetAcquire',
    disabled: _this.operateType == 0 ? false : true,
    radio: {
      options: [
        { key: '0', value: '否' },
        { key: '1', value: '是' }
      ]
    },
    change: (val) => {
      console.log(val, '网络获客')
      if (val.value == '1') {
        _this.channelRelated.isTakeLook = '0'
      }
      if (_this.channelRelated.isTakeLook == '1' || _this.channelRelated.isNetAcquire == '1') {
        let _index = arr.findIndex((item) => {
          return item.key == 'cooperationType'
        })
        if (_index == '-1') {
          arr.splice(4, 0, cooperationTypeObj, ...p3)
        }
        if (_this.channelRelated.cooperationType == 1) {
          // arr.splice(4, arr.length)
          // p3[0].rules = rules()
          // p3[1].rules = rules()
          arr.splice(7, arr.length, ...p4, ...p5)
        } else {
          arr.splice(7, arr.length, ...p5)
        }
      } else {
        arr.splice(4, arr.length, ...p5)
      }
    },
    rules: [
      {
        required: true,
        message: '请选择是否网络获客',
        trigger: 'blur'
      }
    ]
  }

  let cooperationTypeObj = {
    title: '渠道合作类型',
    placeholder: '请选择',
    tips:
      _this.operateType == '0'
        ? `合作类型设置后<span>不可修改</span>，如选择“同策合作”则必须上传渠道带看单`
        : null,
    tipsIsBlock: 'Dinline',
    key: 'cooperationType',
    select: {
      options: cooperationTypeFinal
    },
    disabled: !_this.channelRelated.cooperationType || _this.operateType == 0 ? false : true,
    change: (val) => {
      console.log(val, 'vvvvvaaa')

      if (val.value == 1) {
        // arr.splice(4, arr.length)
        // p3[0].rules = rules()
        // p3[1].rules = rules()

        arr.splice(7, arr.length, ...p4, ...p5)
      } else {
        arr.splice(7, arr.length, ...p5)
        // arr.splice(4, arr.length)
        // p3[0].rules = [{
        //   required: false,
        //   message: '',
        //   trigger: 'blur'
        // }]
        // p3[1].rules = [{
        //   required: false,
        //   message: '',
        //   trigger: 'blur'
        // }]
        // arr.push(...p3)
        // arr.push(...p5)
      }
    },
    rules: [ruleOpt.required('合作类型不能为空')]
  }

  arr.push(...p1) //渠道名称
  arr.push(...p6) //自获客打法
  arr.push(isTakeLookObj) //是否渠道带看
  arr.push(isNetAcquireObj) //是否网络获客
  arr.push(...p5) //是否网络获客

  if (_this.channelRelated.isTakeLook == '1' || _this.channelRelated.isNetAcquire == '1') {
    let _index = arr.findIndex((item) => {
      return item.key == 'cooperationType'
    })
    if (_index == '-1') {
      arr.splice(4, 0, cooperationTypeObj, ...p3)
    }
    if (_this.channelRelated.cooperationType == 1) {
      // arr.splice(4, arr.length)
      // p3[0].rules = rules()
      // p3[1].rules = rules()
      arr.splice(7, arr.length, ...p4, ...p5)
    } else {
      arr.splice(7, arr.length, ...p5)
    }
  } else {
    arr.splice(4, arr.length, ...p5)
  }

  // 合作类型

  // if (_this.channelRelated.cooperationType == 1 || _this.channelRelated.cooperationType ==
  //   6) {
  //   p3[0].rules = rules()
  //   p3[1].rules = rules()
  //   arr.push(...p3)
  //   arr.push(...p4)
  //   arr.push(...p5)
  // } else {
  //   p3[0].rules = [{
  //     required: false,
  //     message: '',
  //     trigger: 'blur'
  //   }]
  //   p3[1].rules = [{
  //     required: false,
  //     message: '',
  //     trigger: 'blur'
  //   }]
  //   arr.push(...p3)
  //   arr.push(...p5)
  // }

  // // 渠道分类
  // if (_this.channelRelated.channelType == 2 || _this.channelRelated.channelType == 3) {
  //   arr.push(cooperationTypeObj)

  //   // 合作类型
  //   if (_this.channelRelated.cooperationType == 1 || _this.channelRelated.cooperationType ==
  //     6) {
  //     p3[0].rules = rules()
  //     p3[1].rules = rules()
  //     arr.push(...p3)
  //     arr.push(...p4)
  //     arr.push(...p5)
  //   } else {
  //     p3[0].rules = [{
  //       required: false,
  //       message: '',
  //       trigger: 'blur'
  //     }]
  //     p3[1].rules = [{
  //       required: false,
  //       message: '',
  //       trigger: 'blur'
  //     }]
  //     arr.push(...p3)
  //     arr.push(...p5)
  //   }
  // } else {
  //   arr.push(...p5)
  // }

  return arr
}

export default {
  options
}
